home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.teleport.com!usenet
- From: GHouck <hksys@teleport.com>
- Newsgroups: comp.lang.c++
- Subject: Re: Return
- Date: 21 Apr 1996 05:10:29 GMT
- Organization: systems hk
- Message-ID: <4lcg05$64r@nadine.teleport.com>
- References: <4landb$345@news.nevada.edu>
- NNTP-Posting-Host: ip-pdx05-35.teleport.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
-
- morrisd@nevada.edu (DAMON MORRIS) wrote:
- >I am having a really bad problem with some return statements...
- >I declare a function to return an int value then in the function
- >I check to see if two strings are equal and if so then I use another
- >variable to see what to return...
- >
- >int charisma(int ability, char *choice)
- > {
- > if (choice == "Loyalty Base")
- > {
- > if (ability==10) return 20;
- > }
- > }
- >
- >This little program will never work for some reason. When I call it
- >like this charisma(10,"Loyalty Base") it always returns some number
- >that it shouldn't. The number that is returned is usually between
- >5000-10000. If I wrote the value of choice to the screen before it
- >checked to see if it was equal to "Loyalty Base" it always says that
- >it is equal to "Loyalty Base", yet it never gets to the second if.
- >I have no idea what is the matter...I will send anyone full source if
- >needed to fix the problem, but the source is 1000+ lines....
- >
- Damon,
-
- The problem in this piece of code is not with the returns,
- but rather with your string comparison. In C/C++, you compare
- two strings (typically) with 'strcmp()'. When you compare
- 'choice == "Loyalty Base"', you are comparing their
- addresses, not their values:
-
- Try instead:
-
- int charisma(int ability, char *choice)
- {
- if (strcmp(choice,"Loyalty Base") == 0 )
- {
- if (ability==10) return 20;
- }
- }
-
-
- Yours, Geoff Houck
-
-